package com.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.bean.Dept;
import com.bean.Emp;
import com.service.DeptService;
import com.service.EmpService;

@Controller
@RequestMapping("emp")
public class EmpController {
	@Autowired
	private DeptService deptService;
	@Autowired
	private EmpService empService;
	@RequestMapping("list")
	public String emplist(Model model){
		List<Emp> list=empService.listAll();
		model.addAttribute("list",list);//request.setAttribute("list",list)
		return "empList"; 
	}
	
	@RequestMapping("toAddDept")
	public String toAddDept(){
		return "addDept";
	}
	
	@RequestMapping("toAddEmp")
	public String toAddEmp(Model model){
		List<Dept> list=deptService.listAll();
		for (Dept dept : list) {
			System.out.println(dept.getDname());
		}
		model.addAttribute("list",list);
		return "addEmp";
	}
	
	
	
	@RequestMapping("empSave")
	public String empSave(Emp emp,@RequestParam("file")MultipartFile file,HttpServletRequest request){
		//获取项目中的upload的路径
				String path=request.getRealPath("/");//获取项目的路径
				File file1=new File(path);
				String parent=file1.getParent();//项目的根目录 
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
				parent=parent+"/upload/"+sdf.format(new Date());
				File dir=new File(parent);
				if(!dir.exists()){//如果当前目录不存在
					dir.mkdirs();//创建一个空的目录
				}
				//获取上传文件名称
				String filename=file.getOriginalFilename();
				//第一个参数表示第二个参数所在的目录
				File newfile=new File(dir,filename);
				if(!newfile.exists()){
					try{
						newfile.createNewFile();//创建一个空的文件
						//将上传的文件内容拷贝到新创建的文件中
						file.transferTo(newfile);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
				emp.setPhoto("/upload/"+sdf.format(new Date())+"/"+file.getOriginalFilename());
				this.empService.saveEmp(emp);
				return "redirect:/emp/list";
			}
	
	@RequestMapping("init")
	public String init(@RequestParam(value="id",defaultValue="0",required=false)String id,Model model){
		Emp emp=empService.getById(Integer.valueOf(id));
		model.addAttribute("emp",emp);
		List<Dept> list=deptService.listAll();
		model.addAttribute("list",list);
		return "empEdit";
	}
	
	@RequestMapping("empUpdate")
	public String empUpdate(Emp emp,HttpServletRequest request,@RequestParam("file")MultipartFile file){
		if(file.getOriginalFilename()!=null&&!file.getOriginalFilename().equals("")){
			//获取项目中的upload的路径
			String path=request.getRealPath("/");//获取项目的路径
			File file1=new File(path);
			String parent=file1.getParent();//项目的根目录 
			SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
			parent=parent+"/upload/"+sdf.format(new Date());
			File dir=new File(parent);
			if(!dir.exists()){//如果当前目录不存在
				dir.mkdirs();//创建一个空的目录
			}
			//获取上传文件名称
			String filename=file.getOriginalFilename();
			//第一个参数表示第二个参数所在的目录
			File newfile=new File(dir,filename);
			if(!newfile.exists()){
				try{
					newfile.createNewFile();//创建一个空的文件
					//将上传的文件内容拷贝到新创建的文件中
					file.transferTo(newfile);
				   }catch(Exception e){
					e.printStackTrace();
				}
			}
			emp.setPhoto("/upload/"+sdf.format(new Date())+"/"+file.getOriginalFilename());
		}
		this.empService.update(emp);
		return "redirect:/emp/list";
	}
	
	@RequestMapping("delete")
	public String delete(String id){
		System.out.println(id);
		empService.delete(Integer.valueOf(id));
		return "redirect:/emp/list";
	}
}
